/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.domain.TrainDmjyhx;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.service.TrainDmjyhxService;
import me.zhengjie.domain.vo.TrainDmjyhxQueryCriteria;
import me.zhengjie.mapper.TrainDmjyhxMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author huangwencong
* @date 2024-10-07
**/
@Service
@RequiredArgsConstructor
public class TrainDmjyhxServiceImpl extends ServiceImpl<TrainDmjyhxMapper, TrainDmjyhx> implements TrainDmjyhxService {

    private final TrainDmjyhxMapper trainDmjyhxMapper;

    @Override
    public TrainDmjyhx findOne(Long trainId, Long trainItemId) {
        return trainDmjyhxMapper.findOne(trainId, trainItemId);
    }

    @Override
    public PageResult<TrainDmjyhx> queryAll(TrainDmjyhxQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(trainDmjyhxMapper.findAll(criteria, page));
    }

    @Override
    public List<TrainDmjyhx> queryAll(TrainDmjyhxQueryCriteria criteria){
        return trainDmjyhxMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(TrainDmjyhx resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(TrainDmjyhx resources) {
        TrainDmjyhx trainDmjyhx = getById(resources.getId());
        trainDmjyhx.copy(resources);
        saveOrUpdate(trainDmjyhx);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Long> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<TrainDmjyhx> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (TrainDmjyhx trainDmjyhx : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("训练ID", trainDmjyhx.getTrainId());
            map.put("训练项目ID", trainDmjyhx.getTrainItemId());
            map.put("训练前(心率|呼吸|血压|心电图)", trainDmjyhx.getXlq());
            map.put("4.0-6.0KPa 1min (心率|呼吸|血压|心电图)", trainDmjyhx.getXl461());
            map.put("4.0-6.0KPa 2min(心率|呼吸|血压|心电图)", trainDmjyhx.getXl462());
            map.put("4.0-6.0KPa 3min(心率|呼吸|血压|心电图)", trainDmjyhx.getXl463());
            map.put("4.0-6.0KPa 卸压后3-5min", trainDmjyhx.getXl46Xyh());
            map.put("6.0-8.0KPa", trainDmjyhx.getXl68());
            map.put("6.0-8.0KPa 卸压后3-5min", trainDmjyhx.getXl68Xyh());
            map.put("8.0-10.0KPa", trainDmjyhx.getXl810());
            map.put("8.0-10.0KPa 卸压后3-5min", trainDmjyhx.getXl810Xyh());
            map.put("主观症状", trainDmjyhx.getFeels());
            map.put("结果判定", trainDmjyhx.getResult());
            map.put("建议", trainDmjyhx.getJy());
            map.put("记录日期", trainDmjyhx.getDate());
            map.put("记录人", trainDmjyhx.getCreateBy());
            map.put("创建时间", trainDmjyhx.getCreateTime());
            map.put("更新时间", trainDmjyhx.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}